<?php
function exportMembers() {
	global $dbname, $db, $db_selected;
	
	$sql = "SELECT FirstName,LastName,Email1 
			FROM cdc_ContactClientDemographics
			WHERE Email1 <> ''
			ORDER BY LastName";
	$result = mysql_db_query($dbname,$sql);
	
	$csvData = '';
	while($value = mysql_fetch_assoc($result)) {
		$line = $value['FirstName'].','.$value['LastName'].','.$value['Email1'];
		$csvData .= trim($line)."\n";
	}
	$csvData = str_replace("\r","",$csvData);
	serveExport($csvData,'members.csv','application/vnd.ms-excel');
}

function exportEventRegistrants($eventID) {
	global $dbname, $db, $db_selected;
	$memberData = '';
	
	$sql = "SELECT EventName,StartDate,EndDate from cdc_Calendar WHERE EventID = '$eventID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	$eventTitle = stripslashes($row['EventName']);
     $eventDateStart = date('M j Y', strtotime($row['StartDate']));
	
	$sql = "SELECT D.FirstName,D.LastName,D.Email1,
                    D.Address,D.City,D.State,D.Zip,D.Phone1
			FROM 
				cdc_ContactClientDemographics D
			LEFT JOIN 
				cdc_linkEventMember EM ON D.MemberID = EM.MemberID
			WHERE
				EM.EventID = '$eventID'
			ORDER BY LastName ASC";
	$result = mysql_db_query($dbname,$sql);

     $csvData = '';
     if (mysql_num_rows($result)>0) {
          $csvData .= $eventTitle.' '.$eventDateStart."\n";
          while($value = mysql_fetch_assoc($result)) {
               $line = DBToCSV($value['FirstName']).','.DBToCSV($value['LastName']).','.$value['Email1'].','.$value['Phone1'].','.DBToCSV($value['Address']).','.$value['City'].','.$value['State'].','.$value['Zip'];
               $csvData .= trim($line)."\n";
          }
          $csvData = str_replace("\r","",$csvData);
     } else {
          $csvData = 'Nobody is registered for this event yet.';
     }
	serveExport($csvData,$eventTitle.'_registration.csv','application/vnd.ms-excel');

	if (mysql_num_rows($result)>0 && $delbert = 104376) {
		$memberData .= '
			<html xmlns:v="urn:schemas-microsoft-com:vml"
			xmlns:o="urn:schemas-microsoft-com:office:office"
			xmlns:w="urn:schemas-microsoft-com:office:word"
			xmlns="http://www.w3.org/TR/REC-html40">
			
			<head>
			<meta http-equiv=Content-Type content="text/html; charset=utf-8">
			<meta name=ProgId content=Word.Document>
			<meta name=Generator content="Microsoft Word 9">
			<meta name=Originator content="Microsoft Word 9">
			<!--[if !mso]>
			<style>
			v\:* {behavior:url(#default#VML);}
			o\:* {behavior:url(#default#VML);}
			w\:* {behavior:url(#default#VML);}
			.shape {behavior:url(#default#VML);}
			</style>
			<![endif]-->
			<title>Your Plan Book</title>
			<!--[if gte mso 9]><xml>
			 <w:WordDocument>
			  <w:View>Print</w:View>
			  <w:DoNotHyphenateCaps/>
			  <w:PunctuationKerning/>
			  <w:DrawingGridHorizontalSpacing>9.35 pt</w:DrawingGridHorizontalSpacing>
			  <w:DrawingGridVerticalSpacing>9.35 pt</w:DrawingGridVerticalSpacing>
			 </w:WordDocument>
			</xml><![endif]-->
			<style>
			<!--
			 /* Font Definitions */
			font-face
				{font-family:Verdana;
				panose-1:2 11 6 4 3 5 4 4 2 4;
				mso-font-charset:0;
				mso-generic-font-family:swiss;
				mso-font-pitch:variable;
				mso-font-signature:536871559 0 0 0 415 0;}
			 /* Style Definitions */
			p.MsoNormal, li.MsoNormal, div.MsoNormal
				{mso-style-parent:"";
				margin:0in;
				margin-bottom:.0001pt;
				mso-pagination:widow-orphan;
				font-size:7.5pt;
			        mso-bidi-font-size:8.0pt;
				font-family:"Verdana";
				mso-fareast-font-family:"Verdana";}
			p.small
				{mso-style-parent:"";
				margin:0in;
				margin-bottom:.0001pt;
				mso-pagination:widow-orphan;
				font-size:1.0pt;
			        mso-bidi-font-size:1.0pt;
				font-family:"Verdana";
				mso-fareast-font-family:"Verdana";}
			@page Section1
				{size:8.5in 11.0in;
				margin:1.0in 1.25in 1.0in 1.25in;
				mso-header-margin:.5in;
				mso-footer-margin:.5in;
				mso-paper-source:0;}
			div.Section1
				{page:Section1;}
			-->
			</style>
			<!--[if gte mso 9]><xml>
			 <o:shapedefaults v:ext="edit" spidmax="1032">
			  <o:colormenu v:ext="edit" strokecolor="none"/>
			 </o:shapedefaults></xml><![endif]--><!--[if gte mso 9]><xml>
			 <o:shapelayout v:ext="edit">
			  <o:idmap v:ext="edit" data="1"/>
			 </o:shapelayout></xml><![endif]-->
			<div style="width: 100%; text-align: center;"><h1>Event Registrations</h1><h2>'.$eventTitle.'</h2></div>
			<table style="border: none;">	
		';
		
		while($value = mysql_fetch_assoc($result)) {
			$fullName = $value['LastName'].', '.$value['FirstName'];
			$email    = $value['Email1'];
			if (!empty($email)) {
				$email = '('.$email.')';
			}
			$memberData .= '
				<tr style="border: none;">
				<td>_______</td>
				<td style="font-family: verdana; font-size: 1.2em;">'.$fullName.'</td>
                    <td style="font-family: verdana; font-size: 1.2em;">'.$email.'</td>
                    <td style="font-family: verdana; font-size: 1.2em;"></td>';
			$memberData .= '</tr>';
		}
		$memberData .= '</table></body></html>';
	} else {
		//$memberData .= 'There is nobody registered for this event.';
	}
	//serveExport($memberData,$eventTitle.'_registration.doc','application/ms-word');
}

function serveExport($string,$filename,$mimeType) {
	header('Content-Type: '.$mimeType);
	header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
	if (strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')!==false) {
		header('Content-Disposition: attachment; filename="'.$filename.'"');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');		
	} else {
		header('Content-Disposition: attachment; filename="'.$filename.'"');
		header('Pragma: no-cache');		
	}	
	print $string;
}
?>